package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.igexin.sdk.PushManager;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.fragment.GuideFragment;
import com.bxd.baodan.update.UpdateChecker;
import com.bxd.baodan.util.VersionUtil;
import com.bxd.baodan.widget.InsureHelperListDialog;
import com.tencent.TIMCallBack;
import com.tencent.TIMManager;
import com.umeng.analytics.MobclickAgent;

public class SettingActivity extends BaseActivity {
    private TextView newFunction;
    private TextView aboutBxd;
    private TextView exit;
    private TextView version, version1;
    private LinearLayout version_ly;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        setActionBarTitle("系统设置");
        newFunction = (TextView) findViewById(R.id.new_function);
        aboutBxd = (TextView) findViewById(R.id.about_bxd);
        exit = (TextView) findViewById(R.id.exit);
        version = (TextView) findViewById(R.id.version);
        version1 = (TextView) findViewById(R.id.version_1);
        version_ly = (LinearLayout) findViewById(R.id.version_ly);



        version.setText("V" + VersionUtil.getVersionName(this));
        version1.setText("V" + VersionUtil.getVersionName(this));

        version_ly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateChecker.checkForDialog(SettingActivity.this, true);
            }
        });


        newFunction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_0_0_WD_sz_gy");
                String url1 = Constant.root_url + "html/bxddownload/mbxdxiazai";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url1});
            }
        });


        aboutBxd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(App.getContext(), "v6_0_0_WD_sz_xgnjs");
                String url = Constant.root_url + "html/common/protocol";
                //  String url = "http://192.168.11.111:8100/html/common/protocol";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showListDialog();
            }
        });


    }

    /**
     * 显示向导
     */
    private void showGuide() {
        GuideFragment fragment = new GuideFragment();
        fragment.show(getSupportFragmentManager(), "guide");
    }

    private void showListDialog() {
        String[] titles = {"退出保单管家", "退出登录"};
        InsureHelperListDialog insureHelperListDialog = new InsureHelperListDialog(
                this, null, titles, 1, 0);
        insureHelperListDialog.setItemClickListener(new InsureHelperListDialog.ItemClickListener() {

            @Override
            public void onItemClick(int position) {
                switch (position) {
                    case 0:
                        TIMManager.getInstance().logout(new TIMCallBack() {
                            @Override
                            public void onError(int i, String s) {

                            }

                            @Override
                            public void onSuccess() {
                                context.sendBroadcast(new Intent(Constant.ACTION_LOGIN_LOGINOUT));
                                //   Log.e("执行", "im退出: ");
                            }
                        });
                        SettingActivity.this.finish();
                        App.getContext().exit();
                        break;
                    case 1:
                        String alias = "wenwen"
                                + LoginInfo.getInstance(context).getUser()
                                .getU_id();
                        PushManager.getInstance().unBindAlias(context, alias, true);
                        LoginInfo.getInstance(App.getContext())
                                .cancelLogin(
                                        SettingActivity.this);

                        App.answer_msg = 0;
                        App.sys_msg = 0;

                        TIMManager.getInstance().logout(new TIMCallBack() {
                            @Override
                            public void onError(int i, String s) {

                            }

                            @Override
                            public void onSuccess() {
                                context.sendBroadcast(new Intent(Constant.ACTION_LOGIN_LOGINOUT));
                            }
                        });
                        SettingActivity.this.finish();
                        break;
                    default:
                        break;

                }


            }
        });
        insureHelperListDialog.show();
    }


}
